#!/usr/bin/env python
# -*- coding: utf-8 -*-

import wx
try:
    from wx.aui import AuiNotebook
except:
    from wx.lib.agw.aui import AuiNotebook
#from wx.lib.agw.aui import AuiNotebook
import wx.lib.rcsizer  as rcs
from core import Core
import uuid
from core.ide.commandmanager import CommandManager
from core.utils.cmdenum import NOTEBOOK_PAGE_CHANGED
from core.utils.singleton import singleton
#TODO renommer


@singleton
class CentralPanel(wx.Panel):
    def __init__(self,parent):
        wx.Panel.__init__(self,parent, -1)
        self._sizer = rcs.RowColSizer()
        self.SetSizer(self._sizer)
        
        self.notebook=Notebook(self)
        self.setIteam(self.notebook,0,0,True)
        
        
    def setIteam(self,wxObj,col,row,growable=False):
        self._sizer.Add(wxObj, 1, wx.EXPAND,row=row,col=col)
        if(growable):
            self._sizer.AddGrowableCol(col)
            self._sizer.AddGrowableRow(row)
        self._sizer.RecalcSizes()
    
    def addPage(self,wxObject,title="Unknow"):
        self.notebook.AddPage(wxObject,wxObject.getTabTitle(),True)
        #self.id=self.notebook.GetPageCount()-1
        
        #self.notebook.SetPageText(0,  self.notebook.GetPageText(0))
        #self._sizer.RecalcSizes()
        #Core().getMainFrame().update()
        
    def getPages(self):
        return self.notebook.GetTabContainer().GetPages()

    def getPageCount(self):
        return self.notebook.GetPageCount()

    def getPagetAt(self,index):
        return self.notebook.GetPage(index)
    
    def selectPage(self,index):
        self.notebook.SetSelection(index)
    
    def getCurrentPage(self):
        return self.notebook.GetCurrentPage()
        
    def hasPage(self):
        return self.notebook.GetPageCount()>0
        
class Notebook(AuiNotebook):
    def __init__(self,parent):
        AuiNotebook.__init__(self,parent,style=wx.NO_BORDER | wx.aui.AUI_NB_DEFAULT_STYLE | wx.aui.AUI_NB_TAB_EXTERNAL_MOVE)
        self.Bind(wx.aui.EVT_AUINOTEBOOK_PAGE_CHANGED,self.onPageChanged)
        self.Bind(wx.aui.EVT_AUINOTEBOOK_PAGE_CHANGING,self.onPageChanging)
    
    def onPageChanged(self,e):
        #print (1,self.GetCurrentPage()._tabtitle)
        w=self.GetCurrentPage()
        if not w.isEnable:
            w.enable()
            CommandManager().execute(NOTEBOOK_PAGE_CHANGED)
        e.Skip()
        
    def onPageChanging(self,e):
        #print (2,self.GetCurrentPage()._tabtitle)
        try:
            w=self.GetCurrentPage()
            if w.isEnable:
                w.disable()
        except:
            pass
        e.Skip()
    
    
    
class CentralPage():
    def __init__(self,tab_title='unnamed',window_title='unknown'):
        self._tabtitle=tab_title
        self.wintitle=window_title
        self.isEnable=False

        self.data=None
        #self.uuid=uuid.uuid1().hex
        #self.wxId=-1
        
    def setTabTitle(self,tab_title):
        self._tabtitle=tab_title
        id=CentralPanel().notebook.GetPageIndex(self)
        if id>-1:
            CentralPanel().notebook.SetPageText(id,tab_title)
    
    def getTabTitle(self):
        return self._tabtitle
    
    def enable(self):
        self.isEnable=True
    
    def disable(self):
        self.isEnable=False
    
    

